package top.hcode.hoj.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.mapper.CollQuestionOptionMapper;
import top.hcode.hoj.pojo.entity.coll.CollQuestionOption;
import top.hcode.hoj.pojo.vo.coll.CollQuestionOptionVO;

import java.util.List;

/**
 * @author orangej
 * @since 2025-02-09
 */
@Service
@RequiredArgsConstructor
public class CollQuestionOptionDao extends ServiceImpl<CollQuestionOptionMapper, CollQuestionOption> {

    private final LazyDao dao;

    public List<CollQuestionOptionVO> queryOptionVO(String questionId) {
        return baseMapper.queryOptionVO(questionId);
    }

    public void removeOtherOptions(String questionId, List<String> optionIdList) {
        dao.runSql("delete from coll_question_option where question_id = ? and id not in (?)", questionId, optionIdList);
    }

    @Cacheable(value = "question_answer_option_ids", key = "#questionId")
    public List<String> queryAnswerOptionIds(String questionId) {
        return dao.selectList(String.class, "select id from coll_question_option " +
                "where question_id = ? and is_answer = 1 order by sort_order", questionId);
    }

    public void removeByQuestionId(Long id) {
        dao.runSql("delete from coll_question_option where question_id = ?", id);
    }

    public List<CollQuestionOption> queryOptionByCollId(String collId) {
        return lambdaQuery().eq(CollQuestionOption::getCollId, collId)
                .orderByAsc(CollQuestionOption::getQuestionId)
                .orderByAsc(CollQuestionOption::getSortOrder)
                .list();
    }

    public List<CollQuestionOption> queryByQuestionId(String questionId) {
        return lambdaQuery().eq(CollQuestionOption::getQuestionId, questionId)
                .orderByAsc(CollQuestionOption::getSortOrder)
                .list();
    }
}
